//
//  ViewController.m
//  20509-NSOperation-图片下载
//
//  Created by student on 16/3/24.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "ViewController.h"
#define kTap (([UIScreen mainScreen].bounds.size.width - 300) / 4)

@interface ViewController ()
@property (nonatomic,strong) NSArray *arr;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/design.jpg",
                 @"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
    self.activityView.hidesWhenStopped = YES;
}
- (IBAction)pneumoniaBtnPressed:(UIButton *)sender {
    //创建一个队列
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [self.activityView startAnimating];
 
    //创建操作任务
    for (int i = 0; i < self.arr.count; i++) {
        //创建ImageView
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kTap * (i%3+1) + 100*(i % 3), 80+(100+kTap)*(i / 3), 100, 100)];
        NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
            NSURL *url = [NSURL URLWithString:self.arr[i]];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *imge = [UIImage imageWithData:data];
            NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
            [mainQueue addOperationWithBlock:^{
            [imageView setImage:imge];
            [self.view addSubview:imageView];
                if (i == self.arr.count - 1) {
                    [self.activityView stopAnimating];
                }
            }];
        }];
        [queue addOperation:op];
    }
}
- (IBAction)serialBtnPressed:(UIButton *)sender
{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = 1;
    [self.activityView startAnimating];
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
        for (int i = 0; i < self.arr.count; i++)
        {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kTap * (i%3+1) + 100*(i % 3), 80+(100+kTap)*(i / 3), 100, 100)];
                NSURL *url = [NSURL URLWithString:self.arr[i]];
                NSData *data = [NSData dataWithContentsOfURL:url];
                UIImage *image = [UIImage imageWithData:data];
                NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
                [mainQueue addOperationWithBlock:^{
                    [imageView setImage:image];
                    [self.view addSubview:imageView];
                }];
        }
        NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
        [mainQueue addOperationWithBlock:^{
            [self.activityView stopAnimating];
        }];
    }];
            [queue addOperation:op];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
